<?php
class Application_Model_Log
{
	private $objDb, $m_cache;
	private static $_instance = null;

	public function __construct()
	{
		$this->objDb = Zend_Registry::get('db');
		$this->m_cache = Zend_Registry::get('cache');
	}

	public static function getInstance()
	{
		if (null === self::$_instance) self::$_instance = new self();
		return self::$_instance;
	}

	/**
	 * @author	Ryan <nguyenhoaivien@yahoo.com>
	 * @date	Oc 16, 2013
	 * @param	empty
	 * @return	true/false
	 * @desc	connect to mongodb
	 */
	private function connectMongo($collection='logs')
	{
		static $mongo;
		if(!isset($mongo))
		{
			$mongo = new Mongo('mongodb://'.DBHOST_MONGODB.':'.DBPORT_MONGODB);
			$db = $mongo->selectDB(DBNAME_MONGODB);
			$db->authenticate(DBUSER_MONGODB, DBPASS_MONGODB);
			$mongo = $db->selectCollection($collection);
		}
		return $mongo;
	}

	public function logdata($type, $userid, $action)
	{
		$db = $this->connectMongo();
		$criteria =  array('_id'=>$userid.'_'.$type);
		$arrNew = array('$push'=>array('jobs'=>array('jid'=>(int)$job_id, 'time'=>time())));
		return $db->update($criteria, $arrNew, array('upsert'=>true));
	}
}